﻿using UnityEngine;
using System.Collections;

public class MoveThisAdvanced : MonoBehaviour {
	
	
	public float translationSpeedX = 0;
	public float translationSpeedY = 1;
	public float translationSpeedZ = 0;
	
	public float translationSpeedXRnd;
	public float translationSpeedYRnd;
	public float translationSpeedZRnd;
	
	public float forceX;
	public float forceY;
	public float forceZ;
	
	public float forceXRnd;
	public float forceYRnd;
	public float forceZRnd;
	
	public float gravity = -10;
	
	public float dampening = 1;
	
	private float dampeningTargetAtNextSecond;
	private float dampeningTime = 0;
	
	public float speedTreshold = 0.001f;
	
	private float gravityAdd;

	
	// Use this for initialization
	void Start () {
		forceX += (Random.value*forceXRnd)-(forceXRnd/2);
		forceY += (Random.value*forceYRnd)-(forceYRnd/2);
		forceZ += (Random.value*forceZRnd)-(forceZRnd/2);
	
		translationSpeedX += (translationSpeedXRnd*Random.value)-(translationSpeedXRnd/2);
		translationSpeedY += (translationSpeedYRnd*Random.value)-(translationSpeedYRnd/2);
		translationSpeedZ += (translationSpeedZRnd*Random.value)-(translationSpeedZRnd/2);
	}
		
		// Update is called once per frame
	void Update () {
		if(Mathf.Abs(translationSpeedX)<speedTreshold) {
			translationSpeedX = 0;
		}
		if(Mathf.Abs(translationSpeedY)<speedTreshold) {
			translationSpeedY = 0;
		}
		if(Mathf.Abs(translationSpeedZ)<speedTreshold) {
			translationSpeedZ = 0;
		}
		
		translationSpeedX += Time.deltaTime*forceX;
		translationSpeedY += Time.deltaTime*forceY;
		translationSpeedZ += Time.deltaTime*forceZ;
		
		dampeningTime += Time.deltaTime;
		
		dampeningTargetAtNextSecond = translationSpeedX*dampening;
		translationSpeedX = Mathf.Lerp(translationSpeedX, dampeningTargetAtNextSecond, dampeningTime);
		
		dampeningTargetAtNextSecond = translationSpeedY*dampening;
		translationSpeedY = Mathf.Lerp(translationSpeedY, dampeningTargetAtNextSecond, dampeningTime);
		
		dampeningTargetAtNextSecond = translationSpeedZ*dampening;
		translationSpeedZ = Mathf.Lerp(translationSpeedZ, dampeningTargetAtNextSecond, dampeningTime);
		
		transform.Translate(new Vector3(translationSpeedX,translationSpeedY,translationSpeedZ)*Time.deltaTime);

		gravityAdd += gravity*Time.deltaTime;
		transform.Translate(new Vector3(0, gravityAdd, 0)*Time.deltaTime, Space.World);
	}
}
